﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YuFeng.Models;
using YuFeng.Models.New;
using YuFeng.Services.News;
using YuFeng.Web.Filters;
using YuFeng.Web.Helpers;

namespace YuFeng.Web.Areas.Admin.Controllers
{
    [UserLoginFilter]
    public class NewsController : BaseController
    {
        private readonly INewsService _newsService;
        public NewsController(INewsService newsService)
        {
            _newsService = newsService;
        }
        public ActionResult NewsCenter(string title, int page = 1)
        {
            var news = _newsService.GetPageNews(title, false, page, 10);
            ViewBag.ArticleTitle = title;
            return View(news);
        }
        public ActionResult Create()
        {
            News news = new News();
            return View(news);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(News news)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _newsService.Create(news);
                    return this.ProcessFormatedResult(true, "创建成功！", "NewsCenter");
                }
                else
                {
                    return this.ProcessFormatedResult(false, this.ExtendErrors(), model: news);
                }
            }
            catch (Exception e)
            {
                return this.ProcessFormatedResult(false, e.Message, model: news);
            }
        }
        public ActionResult Edit(string id)
        {
            try
            {
                var news = _newsService.GetNewsByID(id);
                if (news == null)
                {
                    throw new Exception(string.Format("编号为{0}的新闻不存在", id));
                }
                return View(news);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(News news)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _newsService.Update(news);
                    return this.ProcessFormatedResult(true, "编辑成功！", "Edit", null, new { id = news.Id });
                }
                else
                {
                    return this.ProcessFormatedResult(false, this.ExtendErrors(), model: news);
                }
            }
            catch (Exception e)
            {
                return this.ProcessFormatedResult(false, e.Message, model: news);
            }
        }
        [HttpPost]
        public ActionResult DeleteNews(string id)
        {
            try
            {
                var news = _newsService.GetNewsByID(id);
                if (news == null)
                {
                    return ErrorMsg("新闻不存在或已删除！");
                }
                else
                {
                    news.IsDeleted = true;
                    _newsService.Update(news);
                    return SuccessMsg("删除成功！");
                }
            }
            catch (Exception e)
            {
                return ErrorMsg(e.Message);
            }
        }
        public ActionResult Upload()
        {
            var result = FilesHelper.Upload(Utils.AppConst.NewsTitleImgFold);
            return Json(new
            {
                Entities = result
            });
        }
        public ActionResult UploadImgs()
        {
            var result = FilesHelper.Upload(Utils.AppConst.NewsTitleImgFold);
            return Json(new
            {
                link = result[0].PublicUrl
            });
        }

        #region 荣誉资质
        public ActionResult HonorList(string title, int page = 1)
        {
            var honors = _newsService.GetHonors(title, false, page, 10);
            ViewBag.ArticleTitle = title;
            return View(honors);
        }
        public ActionResult CreateHonor()
        {
            News news = new News() { Content = "荣誉资质" };
            return View(news);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateHonor(News honor)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _newsService.Create(honor);
                    return this.ProcessFormatedResult(true, "创建成功！", "HonorList");
                }
                else
                {
                    return this.ProcessFormatedResult(false, this.ExtendErrors(), model: honor);
                }
            }
            catch (Exception e)
            {
                return this.ProcessFormatedResult(false, e.Message, model: honor);
            }
        }
        public ActionResult EditHonor(string id)
        {
            try
            {
                var news = _newsService.GetNewsByID(id);
                if (news == null)
                {
                    throw new Exception(string.Format("编号为{0}的新闻不存在", id));
                }
                return View(news);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditHonor(News honor)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _newsService.Update(honor);
                    return this.ProcessFormatedResult(true, "编辑成功！", "EditHonor", null, new { id = honor.Id });
                }
                else
                {
                    return this.ProcessFormatedResult(false, this.ExtendErrors(), model: honor);
                }
            }
            catch (Exception e)
            {
                return this.ProcessFormatedResult(false, e.Message, model: honor);
            }
        }


        #endregion

        /// <summary>
        /// 公司介绍
        /// </summary>
        public ActionResult Introduce()
        {
            try
            {
                var introduce = _newsService.GetIntroduce() ?? new News() { Title = "公司介绍", Type = (int)AppEnum.NewsType.Introduce };
                return View(introduce);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Introduce(News introduce)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var dbIntroduce = _newsService.GetNewsByID(introduce.Id);
                    if (dbIntroduce == null)
                    {
                        _newsService.Create(introduce);
                    }
                    else
                    {
                        dbIntroduce.TitleImg = introduce.TitleImg;
                        dbIntroduce.Summary = introduce.Summary;
                        dbIntroduce.Content = introduce.Content;
                        dbIntroduce.ModifiedTime = DateTime.Now;
                        _newsService.Update(dbIntroduce);
                    }
                    return this.ProcessFormatedResult(true, "操作成功！", "Introduce", null, new { id = introduce.Id });
                }
                else
                {
                    return this.ProcessFormatedResult(false, this.ExtendErrors(), model: introduce);
                }
            }
            catch (Exception e)
            {
                return this.ProcessFormatedResult(false, e.Message, model: introduce);
            }
        }

        /// <summary>
        /// 联系我们
        /// </summary>
        public ActionResult Contactus()
        {
            try
            {
                var contactus = _newsService.GetContactUs() ?? new News() { Title = "联系我们", Type = (int)AppEnum.NewsType.ContactUs };
                return View(contactus);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Contactus(News contactus)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var dbContactus = _newsService.GetNewsByID(contactus.Id);
                    if (dbContactus == null)
                    {
                        _newsService.Create(contactus);
                    }
                    else
                    {
                        dbContactus.TitleImg = contactus.TitleImg;
                        dbContactus.Summary = contactus.Summary;
                        dbContactus.Content = contactus.Content;
                        dbContactus.ModifiedTime = DateTime.Now;
                        _newsService.Update(dbContactus);
                    }
                    return this.ProcessFormatedResult(true, "操作成功！", "Contactus", null, new { id = contactus.Id });
                }
                else
                {
                    return this.ProcessFormatedResult(false, this.ExtendErrors(), model: contactus);
                }
            }
            catch (Exception e)
            {
                return this.ProcessFormatedResult(false, e.Message, model: contactus);
            }
        }
    }
}